import { DataType } from '@zilliz/milvus2-sdk-node';

class MilvusXiaoming {
    collection_name = `xiaoming`;
    schema = [
        {
            name: 'id',
            description: 'ID field',
            data_type: DataType.Int64,
            is_primary_key: true,
            autoID: true,
        },
        {
            name: 'vector',
            description: 'Vector field',
            data_type: DataType.FloatVector,
            dim: 8,
        },
        {
            name: 'document',
            description: 'VarChar field',
            data_type: DataType.VarChar,
            max_length: 128,
        },
    ];

    constructor(milvusClient) {
        this.checkCollection(milvusClient);
    }

    async createCollection(milvusClient) {
        await milvusClient.createCollection({
            collection_name: this.collection_name,
            fields: this.schema,
        })
    }

    async checkCollection(milvusClient) {
        try {
            const isExist = await milvusClient.hasCollection({ collection_name: this.collection_name });
            if (!isExist.value) { // 如果不存在，则创建一个新的 collection。
                await this.createCollection(milvusClient);
            }
        } catch (err) {
            console.log('err', err);

        }
    }
}



export default MilvusXiaoming;